﻿using System;
using System.Linq;
using System.Windows.Forms;
using System.Collections;
using Relocation.Base;
using Relocation.Com.Tools;
using Relocation.Com;
using System.Diagnostics;
using Relocation.Base.UI;
using Relocation.Data;

namespace Relocation.UI
{

	public partial class HousesWindow : BaseForm
	{
		#region 全局变量

		/// <summary>
		/// 当前窗体Houses实例的house
		/// </summary>
		private House Houses;   //当前Houses对象 			

		#endregion

		#region 构造函数

		public HousesWindow()
			: base()
		{
			InitializeComponent();
			Initialize();
		}

		public HousesWindow(Session _sesson)
			: base(_sesson)
		{
			InitializeComponent();
			Initialize();
		}

		/// <summary>
		/// 加载houses记录
		/// </summary>		
		public HousesWindow(Session _sesson, House houses)
			: this(_sesson)
		{
			Houses = houses;
		}

		#endregion

		#region 初始化、数据加载

		private void Initialize()
		{
			LoadComboBox();
            //this.TextBox_carbarnArea.Tag = new ControlTag(House.GetPropName(t => t.carbarnArea));
            //this.TextBox_carbarnCode.Tag = new ControlTag(House.GetPropName(t => t.carbarnCode));
			this.TextBox_streetArea.Tag = new ControlTag(House.GetPropName(t => t.streetArea));
			this.TextBox_streetCode.Tag = new ControlTag(House.GetPropName(t => t.streetCode));
			this.TextBoxHouse_code.Tag = new ControlTag(House.GetPropName(t => t.house_code), new ValidatorInfo(new ValidatorInfo.ValidatorHandler(HouseCode_Validating)));
			this.TextBoxStoreroom.Tag = new ControlTag(House.GetPropName(t => t.storeroom), new ValidatorInfo(new ValidatorInfo.ValidatorHandler(storeroom_Validating)));
			this.TextBoxRemark.Tag = new ControlTag(House.GetPropName(t => t.remark));
			this.TextBoxArea1.Tag = new ControlTag(House.GetPropName(t => t.area1));
			this.TextBoxArea2.Tag = new ControlTag(House.GetPropName(t => t.area2));
			this.ComboBoxDistrict.Tag = new ControlTag(House.GetPropName(t => t.district));
			this.ButtonSave.Tag = new ControlTag(new RoleInfo(Session.KEY_ROLE_ADMIN, Session.KEY_ROLE_OPERATOR));
		}

		/// <summary>
		/// 初始化ComboBox的选择项
		/// </summary>
		private void LoadComboBox()
		{
			//初始化房屋类型下拉框
			ArrayList HouseAreatypes = new ArrayList();
			HouseAreatypes.Add(new HouseAreatype("50平米", 4));
			HouseAreatypes.Add(new HouseAreatype("70平米", 0));
			HouseAreatypes.Add(new HouseAreatype("90平米", 1));
			HouseAreatypes.Add(new HouseAreatype("110平米", 2));
			HouseAreatypes.Add(new HouseAreatype("130平米", 3));
			HouseAreatypes.Add(new HouseAreatype("储藏室", 5));
			HouseAreatypes.Add(new HouseAreatype("门市", 6));
			this.ComboBox_area1.DataSource = HouseAreatypes;
			this.ComboBox_area1.DisplayMember = "display";
			this.ComboBox_area1.ValueMember = "value";

			//初始化地区下拉框
			this.ComboBoxDistrict.DataSource = this.Session.DataModel.Houses.Where(it => !string.IsNullOrEmpty(it.district)).OrderBy(t => t.updated).Select(t => t.district).Distinct().ToList();

		}

		#endregion

		#region 事件处理
		/// <summary>
		/// 窗体加载
		/// </summary>		
		private void HousesInfo_Load(object sender, EventArgs e)
		{
			if (this.Houses != null)
			{
				this.SetValue(Houses);
			}
		}

		/// <summary>
		/// 保存、修改
		/// </summary>		
		private void ButtonSave_Click(object sender, EventArgs e)
		{
			try
			{
				if (!this.ValidatorAllControls())
					return;

				//新增
				if (this.Houses == null)
				{
					this.Houses = new House();
					this.Houses.house_id = Relocation.Service.DataModel.GenerateID(this.Houses);
					this.Houses.project_id = this.Session.Project.project_id;
					this.Houses.created = System.DateTime.Now;
					this.Houses.isUsed = false;
				}
				this.GetValue(this.Houses);
				this.Houses.updated = System.DateTime.Now;
				Session.DataModel.Save(this.Houses);
				this.FieldIsChanged = false;
				this.DialogResult = DialogResult.OK;
				this.Close();
			} catch (Exception ex)
			{
				MessageBox.Show("保存失败！");
				Log.Error(ex.GetInnerExceptionMessage());
				Debug.WriteLine(ex);
			}
		} 

		#endregion

		#region 验证

		/// <summary>
		/// 安置房号验证
		/// </summary>		
		private ValidatorInfo.ValidatorResult HouseCode_Validating(Control control) {			
				control.Text = control.Text.Trim();
				string value = control.Text;
				if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(this.TextBoxStoreroom.Text.Trim()))
					return new ValidatorInfo.ValidatorResult(false, "必须填写");
				bool isRepeat = value.Equals(this.Houses.house_code) ? false : (
					(this.Houses == null) ?
					Session.DataModel.Houses.Where(it => it.district.Equals(this.ComboBoxDistrict.Text) && it.house_code.Equals(value)).Any()
					:
					Session.DataModel.Houses.Where(it => it.district.Equals(this.ComboBoxDistrict.Text) && it.house_code.Equals(value) && it.house_id.Equals(this.Houses.house_id)).Any()
					);
				return new ValidatorInfo.ValidatorResult(!isRepeat,isRepeat? "安置房号重复":string.Empty);				
			}

		/// <summary>
		/// 储藏室号验证
		/// </summary>
		private ValidatorInfo.ValidatorResult storeroom_Validating(Control control)
		{
			string value = control.Text.Trim();
				if (string.IsNullOrEmpty(value) && string.IsNullOrEmpty(this.TextBoxHouse_code.Text.Trim()))
					return new ValidatorInfo.ValidatorResult(false, "必须填写");
				else
					return new ValidatorInfo.ValidatorResult(true);
		}

		/// <summary>
		/// 填写面积时自动选择户型
		/// </summary>		
		private void Area1_TextChanged(object sender, EventArgs e)
		{
			try
			{
				string value = this.TextBoxArea1.Text.Trim();
				if (string.IsNullOrEmpty(value) || !Validator.Digit(value))
					return;				
				decimal _area1 = Convert.ToDecimal(value);
				if (_area1 <= 50)
					this.ComboBox_area1.SelectedValue = 4;
				else if (_area1  > 50 && _area1 <= 80)
					this.ComboBox_area1.SelectedValue = 0;
				else if (_area1 > 80 && _area1 <= 100)
					this.ComboBox_area1.SelectedValue = 1;
				else if (_area1 > 100 && _area1 <= 120)
					this.ComboBox_area1.SelectedValue = 2;
				else
					this.ComboBox_area1.SelectedValue = 3;
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
			}
		}
	
		#endregion

		#region 其他方法

		/// <summary>
		/// 填充文本框
		/// </summary>
		/// <param name="house"></param>
		private void SetValue(House house)
		{
			base.SetValueToInput(house);
			this.ComboBox_area1.SelectedValue = house.areatype;
		}

		/// <summary>
		/// 从文本框更新
		/// </summary>		
		private void GetValue(House house)
		{
			base.GetValueFromInput(house);
			house.areatype = (int)this.ComboBox_area1.SelectedValue;
		}

		#endregion

	}

	#region 声明
	/// <summary>
	/// 安置房面积类型实体类
	/// </summary>
	public struct HouseAreatype
	{
		private string Name;
		private int Id;
		public HouseAreatype(string name, int id)
		{
			this.Name = name;
			this.Id = id;
		}
		public string Display
		{
			get
			{
				return Name;
			}
		}
		public int Value
		{
			get
			{
				return Id;
			}
		}
	}	

	#endregion



}
